package com.huluobo.module_risk_level.ui.activity

import android.os.Bundle
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.huluobo.appbase.ui.BaseActivity
import com.huluobo.appbase.utils.DialogLoadingUtil
import com.huluobo.module_risk_level.bean.RiskLevelDetailBean
import com.huluobo.module_risk_level.databinding.ActivityRiskLevelBinding
import com.huluobo.module_risk_level.enum.DataTypeEnum
import com.huluobo.module_risk_level.ui.adapter.RiskLevelMessageAdapter
import com.huluobo.module_risk_level.viewmodel.RiskLevelViewModel

/**
 * Created by lc on 2022/12/30.
 */
class RiskLevelActivity : BaseActivity<ActivityRiskLevelBinding>() {
    override fun getViewBinding(): ActivityRiskLevelBinding =
        ActivityRiskLevelBinding.inflate(layoutInflater)

    private val riskLevelViewModel by viewModels<RiskLevelViewModel>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initView()
    }

    private fun initView() {
        mViewBinding.toolBar.setNavigationOnClickListener {
            finish()
        }

        val layoutManager = LinearLayoutManager(this)
        mViewBinding.rvData.layoutManager = layoutManager
        loadData()
    }

    private fun loadData() {
        DialogLoadingUtil.showLoading(this, "请稍后")
        riskLevelViewModel.loadRiskLevelMessage().observe(this, {
            it?.let {
                if (it.error_code == 0) {
                    mViewBinding.bean = it.result
                    val list = mutableListOf<RiskLevelDetailBean>()
                    val highTitle = RiskLevelDetailBean()
                    highTitle.dataType = DataTypeEnum.DATA_IS_HIGH_TITLE.ordinal
                    list.add(highTitle)
                    it.result?.high_list?.let {
                        list.addAll(it)
                    }
                    val middleTitle = RiskLevelDetailBean()
                    middleTitle.dataType = DataTypeEnum.DATA_IS_MIDDLE_TITLE.ordinal
                    list.add(middleTitle)
                    it.result?.middle_list?.let {
                        list.addAll(it)
                    }
                    val riskLevelAdapter = RiskLevelMessageAdapter(list)
                    mViewBinding.rvData.adapter = riskLevelAdapter
                    DialogLoadingUtil.cancel()
                }
            }
        })
    }
}